package com.efamily.ewallet.server;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.efamily.ewallet.client.account.bean.AccountBean;
import com.efamily.ewallet.client.account.bean.AccountBean.Gender;
import com.efamily.ewallet.client.balance.bean.BalanceBean;
import com.efamily.ewallet.client.balance.bean.BalanceSearchConfig;
import com.efamily.ewallet.client.balance.service.BalanceService;
import com.efamily.ewallet.client.common.bean.CommentBean;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class BalanceServiceImpl extends RemoteServiceServlet implements
		BalanceService {

	public PagingLoadResult<BalanceBean> getBalanceBeans(
			AccountBean accountBean, BalanceSearchConfig searchConfig,
			PagingLoadConfig config) {
		List<BalanceBean> beans = loadBalanceBeans();
		if (config.getSortInfo().getSortField() != null) {
			final String sortField = config.getSortInfo().getSortField();
			if (sortField != null) {
				Collections.sort(beans, config.getSortInfo().getSortDir()
						.comparator(new Comparator<BalanceBean>() {
							public int compare(BalanceBean o1, BalanceBean o2) {
								if (sortField.equals("id")) {
									return o1.getId() < o2.getId() ? -1 : (o1
											.getId() == o2.getId()) ? 0 : 1;
								} else if (sortField.equals("date")) {
									return o1.getDate().compareTo(o2.getDate());
								} else if (sortField.equals("expensesCount")) {
									return o1.getExpensesCount() < o2
											.getExpensesCount() ? -1 : (o1
											.getExpensesCount() == o2
											.getExpensesCount()) ? 0 : 1;
								} else if (sortField.equals("expensesAmount")) {
									return o1.getExpensesAmount() < o2
											.getExpensesAmount() ? -1 : (o1
											.getExpensesAmount() == o2
											.getExpensesAmount()) ? 0 : 1;
								} else if (sortField.equals("profitsCount")) {
									return o1.getProfitsCount() < o2
											.getProfitsCount() ? -1 : (o1
											.getProfitsCount() == o2
											.getProfitsCount()) ? 0 : 1;
								} else if (sortField.equals("profitsAmount")) {
									return o1.getProfitsAmount() < o2
											.getProfitsAmount() ? -1 : (o1
											.getProfitsAmount() == o2
											.getProfitsAmount()) ? 0 : 1;
								} else if (sortField.equals("description")) {
									return o1.getDescription().compareTo(
											o2.getDescription());
								}
								return 0;
							}
						}));
			}
		}

		List<BalanceBean> sublist = new ArrayList<BalanceBean>();
		int start = config.getOffset();
		int limit = beans.size();
		if (config.getLimit() > 0) {
			limit = Math.min(start + config.getLimit(), limit);
		}
		for (int i = config.getOffset(); i < limit; i++) {
			sublist.add(beans.get(i));
		}
		return new BasePagingLoadResult<BalanceBean>(sublist, config
				.getOffset(), beans.size());

	}

	public List<CommentBean> getCommentBeans(AccountBean accountBean,
			CommentBean config) {
		List<CommentBean> beans = loadComments();
		if (config == null)
			return beans;
		else
			return config.getReplies();

	}

	private List<BalanceBean> loadBalanceBeans() {
		List<BalanceBean> beans = new ArrayList<BalanceBean>();

		BalanceBean bean1 = new BalanceBean(1, new Date(), 23, 400, 1, 12000);
		beans.add(bean1);
		BalanceBean bean2 = new BalanceBean(2, new Date(), 12, 400, 0, 0);
		beans.add(bean2);
		BalanceBean bean3 = new BalanceBean(3, new Date(), 17, 3000, 0, 0);
		beans.add(bean3);

		return beans;
	}

	private List<CommentBean> loadComments() {
		List<CommentBean> comments = new ArrayList<CommentBean>();
		CommentBean comment1 = new CommentBean();
		comment1.setId(1);
		comment1.setAuthor(new AccountBean(3, "Gobsek", Gender.MALE, 67,
				"Money-keeper", "France", 300000.13));
		comment1.setDate(new Date());
		comment1.setOwner(new AccountBean(3, "Gobsek", Gender.MALE, 67,
				"Money-keeper", "France", 300000.13));
		comment1.setComment("Oh God. I spent a lot of money today.");

		CommentBean comment2 = new CommentBean();
		comment2.setId(2);
		comment2.setAuthor(new AccountBean(2, "O. Twist", Gender.MALE, 12,
				"Unemployed", "Great britain", 3.75));
		comment2.setDate(new Date());
		comment2.setOwner(new AccountBean(3, "Gobsek", Gender.MALE, 67,
				"Money-keeper", "France", 300000.13));
		comment2
				.setComment("Dear friends. Help me please. I wanna eat too much");
		CommentBean comment3 = new CommentBean();
		comment3.setId(3);
		comment3.setAuthor(new AccountBean(3, "Gobsek", Gender.MALE, 67,
				"Money-keeper", "France", 300000.13));
		comment3.setDate(new Date());
		comment3.setOwner(new AccountBean(3, "Gobsek", Gender.MALE, 67,
				"Money-keeper", "France", 300000.13));
		comment3.setComment("I don't have money!");
		CommentBean comment4 = new CommentBean();
		comment4.setId(4);
		comment4.setAuthor(new AccountBean(1, "McDuck, Skrudzh", Gender.MALE, 54,
				"Businesman", "USA", 279237345));
		comment4.setDate(new Date());
		comment4.setOwner(new AccountBean(3, "Gobsek", Gender.MALE, 67,
				"Money-keeper", "France", 300000.13));
		comment4.setComment("I don't have money <b>too</b>...");
		comment1.addReply(comment2);
		comment2.addReply(comment3);
		comment2.addReply(comment4);
		comments.add(comment1);
		return comments;
	}
}
